<?php
/**
 * Created by 财务变动观察者.
 * User: 楚羽幽
 * Date: 2018/2/8
 * Time: 0:38
 * QQ：1113581489
 */
namespace app\common\logic\observer\logic;
use think\Db;

class MemberAccountss implements \SplObserver
{
    //预设结果集
    private $data;
    //执行结果
    private $bool = false;

    //构造函数
    public function __construct($data = [])
    {
        $this->data = $data;
    }

    //获取结果集
    public function GetData()
    {
        return $this->data;
    }

    //执行操作
    public function update(\SplSubject $subject)
    {
        if (!empty($this->data))
        {
            //获取数据
            $uid = isset($this->data['uid']) ? intval($this->data['uid']) : '';//操作用户
            $number = isset($this->data['number']) ? $this->data['number'] : 0;//操作积分或金额
            $status = isset($this->data['status']) ? intval($this->data['status']) : 1;//操作方式：0收益，1支出
            $type = isset($this->data['type']) ? intval($this->data['type']) : 0;//操作类型：0金额，1积分
            $field = isset($this->data['field']) ? $this->data['field'] : 'price';//操作字段
            $remarks = isset($this->data['remarks']) ? $this->data['remarks'] : '';//操作备注

            //操作用户与操作金额为空则禁止操作
            if (empty($uid) || empty($number))
            {
                return;
            }
            //记录明细
            $status1 = $this->CreateInteglog($uid,$number,$status,$remarks,$type);

            //操作财务
            $status2 = $this->UpdateAccount($uid,$number,$status,$field);

            //返回布尔结果
            if ($status1 !== false && $status2 !== false)
            {
                $this->bool = true;
            }else{
                $this->bool = false;
            }
            $this->data = [];
        }
    }

    //返回执行结果
    public function CallbackExec()
    {
        return $this->bool;
    }

    /**
     * 记录积分明细
     * @param $uid 用户ID
     * @param $number 记录金额或积分
     * @param int $status 0收益 1支出
     * @param string $remarks 备注
     * @param int $type 类型：0云积分 复投积分 2消费积分
     * @return bool
     */
    private function CreateInteglog($uid,$number,$status = 1,$remarks = '',$type = 0)
    {
        $integrallog = [
            'create_time'   => time(),
            'uid'           => $uid,
            'status'        => $status,
            'type'          => $type,
            'number'        => $number,
            'remarks'       => $remarks,
        ];
        if (Db::name('seller_store_account_details')->insert($integrallog))
        {
            return true;
        }else{
            return false;
        }
    }
    /**
     * 操作用户积分
     * @param $uid 用户ID
     * @param $intconsumption 操作积分
     * @param int $status 操作方式：0收益 1支出
     * @param string $field 数据库字段：cloud_integral，repeat，consume
     * @return bool
     */
    private function UpdateAccount($uid,$number,$status = 1,$field = 'price')
    {
        if($status == 0)
        {
            $action = Db::name('seller_store_account')->where(['uid'=> $uid])->setInc($field,$number);
        }else{
            $action = Db::name('seller_store_account')->where(['uid'=> $uid])->setDec($field,$number);
        }
        if ($action !== false)
        {
            return true;
        }else{
            return false;
        }
    }
}